package com.asia5b.a5bmall.same.adapter

import androidx.core.content.ContextCompat
import android.widget.LinearLayout
import android.widget.TextView
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5bmall.same.R
import com.asia5b.a5mall.middleware.model.member.CouponModel
import com.asia5b.a5mall.middleware.model.member.PromotionModel
import com.asia5b.a5mall.middleware.utils.TimeHandle
import com.asia5b.a5mall.middleware.utils.WTimeUtils
import com.asia5b.a5mall.middleware.utils.judge
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class GoodsCouponAdapter(list: ArrayList<MultiItemEntity>) :
        BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(list) {
    init {

        addItemType(1, R.layout.item_goodscoupon_adapter)
        addItemType(2, R.layout.item_goodspromotion_adapter)

    }

    override fun convert(helper: BaseViewHolder, item: MultiItemEntity) {

        when(item.itemType)
        {
            1->{
                setCouponView(helper,item as CouponModel)
            }
            2->{
                setPromotionView(helper,item as PromotionModel)
            }
        }

    }

    fun setPromotionView(helper: BaseViewHolder, item: PromotionModel) {
        //类型
        var title = ""
        //内容
        var content = ""

        if (item.minus_vo != null) {//单品立减
            val minus_vo = item.minus_vo!!
            title=ResString.get(R.string.单品立减)
            content=String.format(ResString.get(R.string.立减_数_元),minus_vo.single_reduction_value)
        }else if (item.full_discount_vo != null) {//满减组合
            val full_discount_vo = item.full_discount_vo!!
            title=ResString.get(R.string.满减组合)
            content= String.format(ResString.get(R.string.满_数_减_数_元),
                    full_discount_vo!!.full_money,
                    full_discount_vo!!.point_value)
        }else if (item.half_price_vo != null) {//第二件半价
            title=ResString.get(R.string.第二件半价)
            content= ResString.get(R.string.该商品第二件半价)
        }else if (item.exchange != null) {//积分商城
            val exchange = item.exchange!!
            title=ResString.get(R.string.积分商城)
            content=String.format(ResString.get(R.string.赠送_数_积分),exchange.exchange_point)
        }else if (item.groupbuy_goods_vo != null) {//团购
            title=ResString.get(R.string.团购)
            content=ResString.get(R.string.团购)
        }else if (item.seckill_goods_vo != null) {//秒杀
            val seckill_goods_vo = item.seckill_goods_vo!!
            title= ResString.get(R.string.限时抢购)
            content= String.format(ResString.get(R.string.天_小时_分钟_分钟后开始), TimeHandle.getDay(seckill_goods_vo.distance_start_time.toInt()),
                    TimeHandle.getHour(seckill_goods_vo.distance_start_time.toInt()),
                    TimeHandle.getSecond(seckill_goods_vo.distance_start_time.toInt()))
        }

        helper.setText(R.id.tv_promotion_type, title)
        helper.setText(R.id.tv_promotion_content, content)
    }

    fun setCouponView(helper: BaseViewHolder, item: CouponModel) {

        //设置背景
        val ll_couponlist_price_vessel = helper.getView<LinearLayout>(R.id.ll_couponlist_price_vessel)
        val priceBg = ContextCompat.getDrawable(context, (item.used_status == 0).judge(R.drawable.ic_coupon_unuse, R.drawable.ic_coupon_useed))
        ll_couponlist_price_vessel.background = priceBg

        //优惠额度
        helper.setText(R.id.tv_couponlist_price, String.format("¥%.2f", item.coupon_price))

        //优惠要求额度
        helper.setText(R.id.tv_couponlist_price_min, String.format(ResString.get(R.string.满_数2_元可使用), String.format("%.2f",item.coupon_threshold_price)))

        //店铺性质
        helper.setText(R.id.tv_couponlist_selltype, (item.seller_id == 1).judge(ResString.get(R.string.自营), ResString.get(R.string.店铺)))
        val tv_couponlist_selltype = helper.getView<TextView>(R.id.tv_couponlist_selltype)
        //设置背景
        val sellBg = ContextCompat.getDrawable(context, (item.used_status == 0).judge(R.drawable.rec1_t_blue_2dp, R.drawable.rec1_t_gray_2dp))
        tv_couponlist_selltype.background = sellBg

        //使用说明
        helper.setText(R.id.tv_couponlist_note, String.format("仅可购买%s店铺商品", item.seller_name))

        //使用期限
        val startTime = WTimeUtils.format("${item.start_time * 1000}", WTimeUtils.text_yyyy_MM_dd)
        val endTime = WTimeUtils.format("${item.end_time * 1000}", WTimeUtils.text_yyyy_MM_dd)
        helper.setText(R.id.tv_couponlist_time, String.format("%s-%s", startTime, endTime))

        //立即使用
        helper.setGone(R.id.tv_couponlist_use_now, item.used_status != 0)

        //店铺名称
        helper.setText(R.id.tv_couponlist_shopname, item.seller_name)


        //是否已使用
        helper.setGone(R.id.tv_couponlist_usedicon, item.used_status == 0)
    }

    init {
        addChildClickViewIds(R.id.tv_couponlist_use_now)
        addChildClickViewIds(R.id.tv_couponlist_shopname)
    }

}